package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.service.ICustomerService;
import com.briup.service.impl.ICustomerServiceImpl;
import com.briup.util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    //Servlet:控制层，调用业务层代码
    private final ICustomerService customerService = new ICustomerServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //跳转login.jsp
        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("LoginServlet--start....");
        //获取用户名和密码
        String userName = req.getParameter("username");
        String userPassword = req.getParameter("password");
        //和数据库中的密码进行对比，调用业务层；
        Customer customer = customerService.login(userName, userPassword);  //这里已经把登录的人给查出来了
        System.out.println(userName);
        System.out.println(userPassword);
        if (customer != null) { //查有此人，可以登录
            //将用户的信息放到Session中;
            req.getSession().setAttribute(Constants.USER_SESSION, customer);
            //跳转到主页重定向
            resp.sendRedirect("index.jsp");
        } else {//查无此人，无法登录
            //转发回登录页面，顺带提示它，用户名或者密码错误；
            req.setAttribute("error", "用户名或者密码不正确，憨批！");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }
    }
}

